//
//  SubscribeSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2021/3/18.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "XLPageViewController.h"

#import "RingSubscribeListViewController.h"
#import "MySubscribeViewController.h"


#import "SubscribeSegViewController.h"

@interface SubscribeSegViewController ()
<XLPageViewControllerDelegate,
XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) UIButton *catBtn;

@property (nonatomic, strong) MySubscribeViewController *subscribeStoreVC;
@property (nonatomic, strong) RingSubscribeListViewController *subscribeRingVC;

@end

@implementation SubscribeSegViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {

    self.view.backgroundColor = UIColorHex(0xffffff);
    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"public_back_black"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self initPageViewController];
    self.pageViewController.selectedIndex = self.selectIndex;

    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height);
        make.width.mas_equalTo(60);
    }];
    
    self.catBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.catBtn setTitleColor:UIColorHex(0x333333) forState:UIControlStateNormal];
    [self.catBtn setImage:[UIImage imageNamed:@"sub_shaixuan"] forState:UIControlStateNormal];
    [self.catBtn addTarget:self action:@selector(showCategory) forControlEvents:UIControlEventTouchUpInside];
    self.catBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.catBtn.frame = CGRectMake(0,0,40,30);

    [self.view addSubview:self.catBtn];
    [self.catBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(NavBar_Height);
        make.width.mas_equalTo(60);
    }];
}

- (void)goBack {
    
    [[TT_TabbarViewController shareTabbar] setCurrentIndex:TT_TabbarIndexType_Mine];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)showCategory {
    [self.subscribeStoreVC showCategory];
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xff4242);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
    config.titleNormalFont = [UIFont systemFontOfSize:16];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.showTitleInNavigationBar = false;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 20;
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];

    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(kStatusBarHeight);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    if (index == 0) {
        return self.subscribeStoreVC;
    } else {
        return self.subscribeRingVC;
    }
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [self titles][index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return [self titles].count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {
    if (index == 0) {
        self.catBtn.hidden = NO;
    } else {
        self.catBtn.hidden = YES;
    }
}

#pragma mark -
#pragma mark 标题数据
- (NSArray *)titles {
    return @[@"预约店铺",@"预约对戒"];
}

- (MySubscribeViewController *)subscribeStoreVC {
    if (!_subscribeStoreVC) {
        _subscribeStoreVC = [[MySubscribeViewController alloc] init];
    }
    return _subscribeStoreVC;
}

- (RingSubscribeListViewController *)subscribeRingVC {
    if (!_subscribeRingVC) {
        _subscribeRingVC = [[RingSubscribeListViewController alloc] init];
    }
    return _subscribeRingVC;
}

@end
